//
//  ModuleFirstTravelsDetailsViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravelsDetailsController.h"
#import "ModuleFirstUserHomepageViewController.h"


#import "ModuleFirst_TravelsDetailsBigModel.h"
#import "ModuleFirst_TravelsDetailsDayModel.h"
#import "ModuleFirst_TravelsDetailsNodeModel.h"

#import "ModuleFirst_UserInformationModel.h"

#import "Height.h"


#import "TravelsFirst_TravelsDetailsHeaderTableView.h"
#import "TravelsFirst_TravelsDetailsTableViewCell.h"

#define VIEWW self.view.frame.size.width
#define VIEWH self.view.frame.size.height

#define MyViewW myView.frame.size.width
#define MyViewH myView.frame.size.height

static NSString *const TDTCellIdentifier = @"TDTReuse";
@interface TravelsDetailsController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIImageView *headBackImageView;

@property (nonatomic, strong) UITableView *travelsDetailsTableView;

@property (nonatomic, strong) ModuleFirst_TravelsDetailsBigModel *allDataModel;

@property (nonatomic, strong) NSMutableArray *alltrip_daysArray;

@property (nonatomic, strong) NSMutableArray *allPhotoArray;

@end

@implementation TravelsDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:248 / 255.0 green:110 / 255.0 blue:142 / 255.0 alpha:0.5];
    self.navigationItem.title = @"游记详情";
    
    //调整坐标
    //默认为YES,导航栏的坐标零点从导航之后开始   NO的话坐标从零点开始
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
//    NSLog(@"%@", self.titleModel.name);
    //初始化数组
    self.allDataModel = [[ModuleFirst_TravelsDetailsBigModel alloc]init];
    self.alltrip_daysArray = [NSMutableArray array];
    self.allPhotoArray = [NSMutableArray array];
    
    //调用函数, 获取数据
    [self getAFNetworkingData];
    
    
    //tableView定义
    self.travelsDetailsTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, VIEWW, VIEWH -66) style:UITableViewStylePlain];
//    self.travelsDetailsTableView.backgroundColor = YTColor(236, 240, 245, 1.0);
    [self.view addSubview:self.travelsDetailsTableView];
    
    //消除分割线
    self.travelsDetailsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //成为代理人
    self.travelsDetailsTableView.dataSource = self;
    self.travelsDetailsTableView.delegate = self;
    
    
    //tableView注册
    [self.travelsDetailsTableView registerClass:[TravelsFirst_TravelsDetailsTableViewCell class] forCellReuseIdentifier:TDTCellIdentifier];
    
    
////加头视图
//    [self addTableHeaderView];
//    
    
    //导航栏按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"BackButton_1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBackButton)];
    
    
}

//网络请求数据
- (void)getAFNetworkingData{
    
    
    //创建一个管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    [manager.securityPolicy setValidatesDomainName:NO];
    //识别各种类型的网址
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/trips/%@.json", self.idd];
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"%@", responseObject);
  
        [self.allDataModel setValuesForKeysWithDictionary:responseObject];
        
        
        NSMutableArray *trip_daysArray = self.allDataModel.trip_days;
        
        for (ModuleFirst_TravelsDetailsDayModel *smallDayModel in trip_daysArray) {
            
            [self.alltrip_daysArray addObject:smallDayModel];
            
            NSMutableArray *smallNotes = smallDayModel.nodes;
            
            for (ModuleFirst_TravelsDetailsNodeModel *smallNoteModel in smallNotes) {
                
//                NSLog(@"%@", smallNoteModel.descriptions);
                if ([smallNoteModel.image_width stringValue].length != 0) {
                    
                    [self.allPhotoArray addObject:smallNoteModel.photoUrl];
                }
                
                
            }
            
        }
        
        //加头视图
        [self addTableHeaderView];
        

        
        //刷新tableView
        [self.travelsDetailsTableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error====%@",error);
        
        
    }]; 
    
    
}


//加头视图

- (void)addTableHeaderView{
    
    TravelsFirst_TravelsDetailsHeaderTableView *myTableHeaderView = [[TravelsFirst_TravelsDetailsHeaderTableView alloc]initWithFrame:CGRectMake(0, 0, VIEWW, VIEWW / 4 )];
//    myTableHeaderView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:110 / 255.0 blue:142 / 255.0 alpha:0.3];
    myTableHeaderView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    myTableHeaderView.headerModel = self.allDataModel;
    
    //点击作者头像, 跳转
    __block TravelsDetailsController *root = self;
    myTableHeaderView.toUserHomePage = ^(){
        
        ModuleFirstUserHomepageViewController *MFUHVC = [[ModuleFirstUserHomepageViewController alloc]init];
        UINavigationController *MFUHNaVC = [[UINavigationController alloc ]initWithRootViewController:MFUHVC];
        MFUHVC.userModel = self.allDataModel.userModel;
        [root presentViewController:MFUHNaVC animated:YES completion:^{
            
            
        }];
        
    };
    
    self.travelsDetailsTableView.tableHeaderView = myTableHeaderView;
    
}




//*****必须要做的******
//显示每个cell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TravelsFirst_TravelsDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TDTCellIdentifier];
    
//    cell.backgroundColor = YTColor(236, 240, 245, 1.0);
    ModuleFirst_TravelsDetailsDayModel *daysModel = [self.alltrip_daysArray objectAtIndex:indexPath.section];
    
    ModuleFirst_TravelsDetailsNodeModel *tripModel = [daysModel.nodes objectAtIndex:indexPath.row];
    
    cell.tableCellModel = tripModel;
    
    cell.contentView.layer.cornerRadius = 6;
    cell.contentView.layer.masksToBounds = YES;
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(TravelsFirst_TravelsDetailsTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
       CATransform3D rotation;//3D旋转
    
    rotation = CATransform3DMakeTranslation(0 ,50 ,20);
    rotation = CATransform3DScale(rotation, 0.9, .9, 1);
    
    rotation.m34 = 1.0/ -600;
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    
    cell.layer.transform = rotation;
    
    [UIView beginAnimations:@"rotation" context:NULL];
    //旋转时间
    [UIView setAnimationDuration:0.9];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
    
}




//每个分区中cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    ModuleFirst_TravelsDetailsDayModel *dayModel = [self.alltrip_daysArray objectAtIndex:section];
    
    NSMutableArray *tripsArray = dayModel.nodes;
    
//    NSLog(@"cellNum == %ld",tripsArray.count);
    return tripsArray.count;
    
//    return 20;
    
}


//*****选做的******

//每个 cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ModuleFirst_TravelsDetailsDayModel *daysModel = [self.alltrip_daysArray objectAtIndex:indexPath.section];
    
    ModuleFirst_TravelsDetailsNodeModel *tripModel = [daysModel.nodes objectAtIndex:indexPath.row];
    
    //视频的高度
    CGFloat heightVideo = [Height getHeightImageWithHeight:tripModel.video_height withWidth:tripModel.video_width widthNowImage:VIEWW - 20];
    
    //图片的高度
    CGFloat heightImage = [Height getHeightImageWithHeight:tripModel.image_height withWidth:tripModel.image_width widthNowImage:VIEWW - 20];
    //label的高度
    CGFloat heightLabel = [Height getLabelHeightWithString:tripModel.descriptions widthNowLabel:(VIEWW - 20) fontSize:17 options:NSStringDrawingUsesLineFragmentOrigin];
    
    
    return heightImage + heightLabel + heightVideo + 20;
}



//分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
//    NSLog(@"sectionNum == %ld", self.alltrip_daysArray.count);
    return self.alltrip_daysArray.count;
}

//分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 30;
}


//分区头上的视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEWW, 30)];
    
//    myView.backgroundColor = YTColor(255, 255, 255, 1.0);
    
    
    UILabel *dayLabel = [[UILabel alloc]initWithFrame:CGRectMake(MyViewW / 3, 5, MyViewW / 3 / 3, MyViewH - 10)];
//    dayLabel.backgroundColor = YTColor(255, 255, 255, 1.0);
    [myView addSubview:dayLabel];
    
    dayLabel.text = [NSString stringWithFormat:@"DAY%ld", section + 1];
    
    UILabel *dataLabel= [[UILabel alloc]initWithFrame:CGRectMake(MyViewW / 3 + MyViewW / 3 / 3 + 10, 5, MyViewW / 3 * 2 - 10, MyViewH - 10)];
//    dataLabel.backgroundColor = YTColor(255, 255, 255, 1.0);
    [myView addSubview:dataLabel];
    
    ModuleFirst_TravelsDetailsDayModel *dayModel = [self.alltrip_daysArray objectAtIndex:section];
    dataLabel.text = dayModel.trip_date ;
//    dataLabel.textColor = YTColor(176, 176, 176, 1.0);
    
    return myView;
}


//导航栏左边返回按钮
- (void) leftBackButton{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    
}


@end
